package com.ytktt.study.week9.task7.transaction.service.impl;

import com.ytktt.study.week9.task7.account.api.Account0Service;
import com.ytktt.study.week9.task7.account.api.Account1Service;
import com.ytktt.study.week9.task7.transaction.service.TransactionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hmily.annotation.HmilyTCC;
import org.springframework.stereotype.Service;

/**
 * 交易业务逻辑实现
 *
 * @author ytk
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TransactionServiceImpl implements TransactionService {

    private final Account0Service account0Service;
    private final Account1Service account1Service;

    @Override
    @HmilyTCC(confirmMethod = "confirm", cancelMethod = "cancel")
    public void exchange() {
        this.account0Service.exchangeUSDToCNY();
        this.account1Service.exchangeCNYToUSD();
    }

    @Override
    @HmilyTCC(confirmMethod = "confirm", cancelMethod = "cancel")
    public void exchangeTryException() {
        this.account0Service.exchangeUSDToCNY();
        this.account1Service.mockExchangeCNYToUSDTryException();
    }

    public void confirm() {
        log.info("=========进行交易confirm操作完成================");
    }

    public void cancel() {
        log.info("=========进行交易cancel操作完成================");
    }
}
